package controllers;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JPanel;

import models.Sprite;

@SuppressWarnings("serial")
public class Timer extends JPanel implements Runnable {
	
	public Game game;
	
	private int turn;
	private Sprite timerDec = new Sprite();
	private Sprite timerSec = new Sprite();
	
	private JLabel troopLabel = new JLabel("Troops: ");
	private JLabel numberTroopsLabel = new JLabel("");
	private JLabel resourcesLabel = new JLabel("Resource: ");
	private JLabel numberResourcesLabel = new JLabel("");	
	private JLabel healthLabel = new JLabel("Health: ");
	private JLabel healthLevelLabel = new JLabel("");
	private JLabel timerSpace = new JLabel();
	
	private int dec;
	private int sec;

	public Timer(Game game) {
		this.turn = game.turn;
		this.game = game;
		
		
		
		dec = turn/10;
		sec = turn%10;
		
		timerDec.setFrame(dec);
		timerSec.setFrame(sec);
		
		//timerDec.setStatus(0);
		//timerSec.setStatus(0);
		
		initializeTimer();
		this.setLayout(new GridLayout());
		
		addElements();
		
		
		new Thread(this).start();
	}
	
	

	private void addElements() {
		
		GridBagConstraints constraints = new GridBagConstraints();
		
		//Troop Label
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 2;
		constraints.gridheight = 1;
		constraints.weightx = 8.0;
		constraints.weighty = 0.6;
		constraints.fill = GridBagConstraints.BOTH;
		
		this.add(timerSpace, constraints);
		
		//Number of Troops Label
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 2;
		constraints.gridheight = 1;
		constraints.weightx = 8.0;
		constraints.weighty = 0.6;
		constraints.fill = GridBagConstraints.BOTH;
		
		this.add(troopLabel, constraints);
				
		//Resources Label
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridwidth = 2;
		constraints.gridheight = 1;
		constraints.weightx = 8.0;
		constraints.weighty = 0.6;
		constraints.fill = GridBagConstraints.BOTH;
		
		this.add(numberTroopsLabel, constraints);

		//Number of resources Label
		constraints.gridx = 0;
		constraints.gridy = 3;
		constraints.gridwidth = 2;
		constraints.gridheight = 1;
		constraints.weightx = 8.0;
		constraints.weighty = 0.6;
		constraints.fill = GridBagConstraints.BOTH;
		
		this.add(resourcesLabel, constraints);
				
		//Health Label
		constraints.gridx = 0;
		constraints.gridy = 4;
		constraints.gridwidth = 2;
		constraints.gridheight = 1;
		constraints.weightx = 8.0;
		constraints.weighty = 0.6;
		constraints.fill = GridBagConstraints.BOTH;
		
		this.add(numberResourcesLabel, constraints);
				
		//Health level Label
		constraints.gridx = 0;
		constraints.gridy = 5;
		constraints.gridwidth = 2;
		constraints.gridheight = 1;
		constraints.weightx = 8.0;
		constraints.weighty = 0.6;
		constraints.fill = GridBagConstraints.BOTH;
		
		this.add(healthLabel, constraints);
		
		//Timer hole
		constraints.gridx = 0;
		constraints.gridy = 6;
		constraints.gridwidth = 2;
		constraints.gridheight = 1;
		constraints.weightx = 8.0;
		constraints.weighty = 0.6;
		constraints.fill = GridBagConstraints.BOTH;
		
		this.add(healthLevelLabel, constraints);
				
	}



	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		super.paintComponent(g2d);
		
		numberTroopsLabel.setText(""+game.player1.numTroops);
		numberResourcesLabel.setText(""+game.player1.resources);
		healthLevelLabel.setText(""+game.player1.health);
		
		timerDec.draw(g2d);
		timerSec.draw(g2d);
	}



	private void initializeTimer() {
		timerDec.setWidth(30);
		timerDec.setHeight(50);
		timerSec.setWidth(30);
		timerDec.setHeight(50);
		
		
		try {
			timerDec.addFrame("/images/numbers/0.jpg", 0, 0);
			timerDec.addFrame("/images/numbers/1.jpg", 0, 1);
			timerDec.addFrame("/images/numbers/2.jpg", 0, 2);
			timerDec.addFrame("/images/numbers/3.jpg", 0, 3);
			timerDec.addFrame("/images/numbers/4.jpg", 0, 4);
			timerDec.addFrame("/images/numbers/5.jpg", 0, 5);
			timerDec.addFrame("/images/numbers/6.jpg", 0, 6);
			timerDec.addFrame("/images/numbers/7.jpg", 0, 7);
			timerDec.addFrame("/images/numbers/8.jpg", 0, 8);
			timerDec.addFrame("/images/numbers/9.jpg", 0, 9);
			
			timerSec.addFrame("/images/numbers/0.jpg", 0, 0);
			timerSec.addFrame("/images/numbers/1.jpg", 0, 1);
			timerSec.addFrame("/images/numbers/2.jpg", 0, 2);
			timerSec.addFrame("/images/numbers/3.jpg", 0, 3);
			timerSec.addFrame("/images/numbers/4.jpg", 0, 4);
			timerSec.addFrame("/images/numbers/5.jpg", 0, 5);
			timerSec.addFrame("/images/numbers/6.jpg", 0, 6);
			timerSec.addFrame("/images/numbers/7.jpg", 0, 7);
			timerSec.addFrame("/images/numbers/8.jpg", 0, 8);
			timerSec.addFrame("/images/numbers/9.jpg", 0, 9);
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		timerDec.setPosX(0);
		timerDec.setPosY(0);
		timerSec.setPosX(timerDec.getWidth());
		timerSec.setPosY(0);
		
	}

	@Override
	public void run() {
		
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		while(true){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			for(int i=turn; i>=0; i--){
				
				if(timerSec.getFrame()>0){
					timerSec.setFrame(timerSec.getFrame()-1);
				}else if((timerSec.getFrame()==0)&&(timerDec.getFrame()>0)){
					timerDec.setFrame(timerDec.getFrame()-1);
					timerSec.setFrame(9);
				}
				
				if(i==0){
					game.tg.addTroopsToTurn();
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
					timerSec.setFrame(sec);
					timerDec.setFrame(dec);
					break;
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
			}	
		}
	}
}
